import { AnyAction, applyMiddleware, legacy_createStore as createStore } from 'redux'

// 导入合并的reducer
import reducers from './reducers'

// 导入redux-devtools-extension扩展工具
import { composeWithDevTools } from 'redux-devtools-extension'
// 导入处理异步请求的thunk中间件
import { ThunkAction, thunk } from 'redux-thunk'
// 创建store并传递reducer
// 参数1 ：合并好的reducer
// 参数2： reducer模块的初始值，非必填
// 参数3，可以传递一个中间件，并使用 applyMiddleware来应用

const store = createStore(reducers, {}, composeWithDevTools(applyMiddleware(thunk)))
// state的类型声明,以便在useSelector中导入类型
export type RootState = ReturnType<typeof store.getState>
// 异步action函数配置类型
export type RootThunkAction = ThunkAction<
  Promise<void>,
  RootState,
  unknown,
  AnyAction
>
export default store
